//
//  ViewController.m
//  20-XML-SAXAndDom
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "XMLReader.h"

@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, strong) NSMutableArray *domStuArray;
@property (nonatomic, copy) NSString *currentElenmentName;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
// XML 分为两种解析方式
// 第一种 SAX 逐行解析 - 系统的解析器NSXMLParser
- (IBAction)saxParse:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    xmlParser.delegate = self;
    [xmlParser parse];
}

// 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 遇到节点头 方法的回调 记录当前节点 方便后续赋值
// 打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    _currentElenmentName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *student = [Student new];
        [_stuArray addObject:student];
    }
    NSLog(@"节点头:%@", elementName);
}
// 节点的值 根据节点决定值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // 第一种
    // string是 \n\t
//    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
//        Student *stu = [_stuArray lastObject];
//        [stu setValue:string forKey:_currentElenmentName];
//
//    }
    if (_currentElenmentName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElenmentName];
    }
}
// 遇到节点尾 方法的回调, 判断当前节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    _currentElenmentName = nil;
    NSLog(@"节点尾:/%@", elementName);
}
// 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@", _stuArray);
}
// 解析失败时调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    
}
#pragma mark - 第二种解析方式 DOM
// 关于GDataXMLNode的引入
// 1 引入.h与.m文件
// 2 引入系统动态库libxml2.tbd
// 3 根据GDataXMLNode头文件的提示 添加/usr/include/libxml2
// 4 如果是ARC环境 需要对该文件进行MRC编译 添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    self.domStuArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    // 根据XML的Data数据 生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    // 根据document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    // 根据根节点获取子节点数组
    NSArray *rootChildElements = rootElement.children;
    // 遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElements) {
        NSLog(@"childElement = %@", childElement.name);
        // student节点
        Student *stu = [Student new];
        [_domStuArray addObject:stu];
        // 遍历student节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            NSLog(@"grandsonElement = %@", grandSonElement.name);
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@", _domStuArray);
    
    NSDictionary *dicXML = [XMLReader dictionaryForXMLData:data error:&error];
    NSLog(@"%@", dicXML);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
